import config from './config.js'

export default function request(options){
	const {url, method, data, header} = options
	const token = uni.getStorageSync('SESSION_ID')
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			method,
			data,
			header: {
				...header,
				token
			},
			success: (res) => {
				if(res.statusCode === 401) {
					uni.removeStorageSync('ROLE')
					uni.removeStorageSync('SESSION_ID')
					uni.reLaunch({
						url: '/pages/index/index',
						success() {
							uni.showToast({
								title: '登录过期，请重新登录',
								icon: 'none'
							})
						}
					})
				} else {
					resolve(res.data)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}